package edu.mju.security.config;

import edu.mju.security.filter.AuthenticationFilter;
import edu.mju.security.filter.LoginFilter;
import edu.mju.security.handler.MyLogoutHandler;
import edu.mju.security.handler.UnauthorizedHandler;
import edu.mju.security.utils.DefaultPasswordUtils;
import edu.mju.security.utils.TokenUtils;
import org.apache.catalina.filters.CorsFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsUtils;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * SpringSecurity核心配置类
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    private TokenUtils tokenUtils;
    private RedisTemplate redisTemplate;
    private DefaultPasswordUtils defaultPasswordUtils;
    private UserDetailsService userDetailsService;

    @Autowired
    public SecurityConfig(TokenUtils tokenUtils,RedisTemplate redisTemplate,DefaultPasswordUtils defaultPasswordUtils,UserDetailsService userDetailsService){
        this.tokenUtils = tokenUtils;
        this.redisTemplate = redisTemplate;
        this.defaultPasswordUtils = defaultPasswordUtils;
        this.userDetailsService = userDetailsService;
    }


    /**
     * 设置未授权处理、关闭csrf保护、请求管理、登出管理、添加自定义过滤器
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //未授权处理
                .exceptionHandling().authenticationEntryPoint(new UnauthorizedHandler())
                //关闭csrf保护
                .and().csrf().disable()
                //请求管理
                .authorizeRequests()
                .anyRequest().authenticated()
                //登出管理
                .and().logout().logoutUrl("/admin/user/index/logout")
                .addLogoutHandler(new MyLogoutHandler(tokenUtils,redisTemplate))
                //添加自定义过滤器
                .and()
                .addFilter(new LoginFilter(tokenUtils,redisTemplate,authenticationManager()))
                .addFilter(new AuthenticationFilter(authenticationManager(),tokenUtils,redisTemplate))
        .httpBasic()
        ;

    }


    /**
     * 配置userDetailsService和密码处理
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(defaultPasswordUtils);
    }

    /**
     * 不进行认证的路径设置
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web     //忽略
                .ignoring().antMatchers("/swagger-ui.html/**", "/swagger-resources/**","/webjars/**", "/v2/**","/api/**");
    }
}
